package com.hry.igoods.controller.portal;

import com.hry.igoods.common.Const;
import com.hry.igoods.common.ResponseCode;
import com.hry.igoods.common.ServerResponse;
import com.hry.igoods.pojo.User;
import com.hry.igoods.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * 用户控制层
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录接口
     *
     * @param username
     * @param password
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    public ServerResponse<User> login(String username, String password, HttpSession session) {
        ServerResponse response = userService.login(username, password);
        if (response.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    /**
     * 登出接口
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "logout.do", method = RequestMethod.GET)
    public ServerResponse<String> logout(HttpSession session) {
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccessMessage("退出成功");
    }

    /**
     * 用户注册接口
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "register.do", method = RequestMethod.GET)
    public ServerResponse<String> register(User user) {
        return userService.register(user);
    }

    /**
     * 数据校验
     *
     * @param str
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "check_valid.do", method = RequestMethod.GET)
    public ServerResponse<String> validResponse(String str, String type) {
        return userService.checkValid(str, type);
    }

    /**
     * 获取用户信息
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.GET)
    public ServerResponse<User> getUserInfo(HttpSession session) {
        // 从session中获取用户信息
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        // 判断用户信息是否为空
        if (user != null) {
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登录，无法获取当前用户信息");
    }

    /**
     * 忘记密码过程中，找到问题的接口
     *
     * @param username
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "forget_get_question.do", method = RequestMethod.GET)
    public ServerResponse<String> forgetGetQuestion(String username) {
        // 根据用户名查询用户找回密码所设置的问题
        return userService.selectQuestion(username);
    }

    /**
     * 使用本地缓存检查问题答案是否正确
     *
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "forget_check_answer.do", method = RequestMethod.GET)
    public ServerResponse<String> forgetCheckAnswer(String username, String question, String answer) {
        return userService.checkAnswer(username, question, answer);
    }

    /**
     * 重置密码
     *
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "forget_reset_password.do", method = RequestMethod.GET)
    public ServerResponse<String> forgetResetPassword(String username, String passwordNew, String forgetToken) {
        return userService.forgetResetPassword(username, passwordNew, forgetToken);
    }

    /**
     * 登录状态下修改密码
     * @param passwordOld
     * @param passwordNew
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "reset_password.do", method = RequestMethod.GET)
    public ServerResponse<String> resetPassword(String passwordOld, String passwordNew, HttpSession session) {
        // 从session中取用户，看用户是否存在
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorMessage("用户还未登录");
        }
        // 调用修改密码的方法
        return userService.resetPassword(passwordOld, passwordNew, user);

    }

    /**
     * 登录状态下更新用户信息
     * @param session
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update_user_information.do", method = RequestMethod.GET)
    public ServerResponse<User> updateUserInformation(HttpSession session, User user) {
        // 检查用户是否登陆
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            return ServerResponse.createByErrorMessage("用户还未登录");
        }
        // 设置用户的id为当前登录用户的id 防止通过URL请求传入其他id更改密码
        user.setId(currentUser.getId());
        // 设置用户名为当前id登陆的用户名
        user.setUsername(currentUser.getUsername());
        ServerResponse<User> response = userService.updateInformation(user);
        if (response.isSuccess()) {
            response.getData().setUsername(currentUser.getUsername());
            // 将更新后的数据写入session中
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    /**
     * 获取用户信息
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "get_user_information.do", method = RequestMethod.POST)
    public ServerResponse<User> getUserInformation(HttpSession session) {
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if (currentUser == null) {
            // 如果用户为空，需要进行登录
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户需要登录status= 10");
        }
        return userService.getUserInformation(currentUser.getId());
    }
}
